package org.mclll.xwangplugin.town.command.executor;

import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.event.ClickEvent;
import net.kyori.adventure.text.event.HoverEvent;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.mclll.xwangplugin.town.database.domain.TownDO;
import org.mclll.xwangplugin.town.database.domain.TownPlayerDO;
import org.mclll.xwangplugin.town.database.mapper.manage.TownManage;
import org.mclll.xwangplugin.town.database.mapper.manage.TownPlayerManage;
import org.mclll.xwangplugin.town.enums.PlayerTownIdentity;
import org.mclll.xwangplugin.town.lp.LuckPermsTown;
import org.mclll.xwangplugin.util.PlayerTools;

import java.util.Collections;
import java.util.List;

import static org.mclll.xwangplugin.town.config.TownContext.*;

/**
 * @author: geduo
 * @date: 2023/9/27 17:24
 */
public class TownNoneLeaderExecutor {

    public static void noneLeader(CommandSender sender, String[] args) {
        // 设置城镇没有leader - noneLeader <城镇名> [confirm]
        if (args.length < 2) {
            sender.sendMessage(NONE_LEADER_COMMAND_FORMAT);
            return;
        }
        if (args.length == 2) {
            // 二次确认
            sender.sendMessage(
                    Component.text(String.format(NONE_LEADER_CONFIRM, args[1]))
                            .appendNewline()
                            .append(
                                    Component.text(CONFIRM_BUTTON)
                                            .clickEvent(ClickEvent.runCommand(String.format("/poitown noneLeader %s confirm", args[1])))
                                            .hoverEvent(HoverEvent.showText(Component.text(CONFIRM_BUTTON)))
                            )
                            .append(Component.text("         "))
                            .append(Component.text(CANCEL_BUTTON).hoverEvent(HoverEvent.showText(Component.text(CANCEL_BUTTON))))
            );
            return;
        }
        if (args.length == 3 && "confirm".equals(args[2])) {
            String townName = args[1];
            TownDO selectParam = new TownDO();
            selectParam.setName(townName);
            List<TownDO> townList = TownManage.selectList(selectParam);
            if (townList.isEmpty()) {
                sender.sendMessage(TOWN_NOT_FOUND);
                return;
            }
            TownDO town = townList.get(0);
            String leaderUuid = town.getLeaderUuid();
            TownDO townUpdateParam = new TownDO();
            townUpdateParam.setUuid(town.getUuid());
            townUpdateParam.setLeaderUuid("-");
            TownManage.updateByUuid(townUpdateParam);
            TownPlayerDO player = TownPlayerManage.selectByUuid(leaderUuid);
            player.setIdentity(PlayerTownIdentity.GENSHIN);
            player.setTownUuid("-");
            TownPlayerManage.updateByUuid(player);
            OfflinePlayer offlinePlayer = PlayerTools.getOfflineOrOnlinePlayerByUuid(leaderUuid);
            // 权限部分
            LuckPermsTown.kickPlayerFromTown(Collections.singletonList(offlinePlayer.getName()));
            LuckPermsTown.delLeader(offlinePlayer.getName());
            if (offlinePlayer.isOnline()) {
                ((Player) offlinePlayer).sendMessage(YOU_HAVE_BEEN_DISMISSED);
            }
            sender.sendMessage(String.format(NONE_LEADER_SUCCESS, townName));
        } else {
            sender.sendMessage(NONE_LEADER_COMMAND_FORMAT);
        }
    }
}
